import { getUserInfo } from '@/static/js/user'
import { smsSend, mobileLogin } from '@/static/js/login'
import { goEasy } from '@/utils/goEasy.js';
import homeApi from "@/static/js/enterpriseServer/index.js"
export default {
	data() {
		return {
			focus: true, //input焦点，用于键盘隐藏后重新唤起
			form: {
				mobile: '', //	是	string	手机号
				captcha: '', //这是用户输入的验证码
				app_version: '' //版本号
			},
			codeColor: '#313131', //自定义光标的颜色
			countDown: 60, //新获取验证码的倒计时
			clock: null, //倒计时计时器
		};
	},
	methods: {
		goBack() {
			uni.navigateBack({
				delta: 1
			});
		},
		// 键盘隐藏后设置失去焦点
		blur() {
			this.focus = false;
		},
		// 点击自定义光标显示键盘
		codeFocus(e) {
			if (e == this.form.captcha.length) {
				this.focus = true;
			}
		},
		// 输入验证码
		codeNum(event) {
			var that = this;
			var code = event.target.value;
			if (code.length != 4) {
				return false;
			}
			this.form.captcha = code;
			this.form.cid = uni.getStorageSync('cid');
			mobileLogin(this.form).then((res) => {
				if (res.code == 1) {
					this.$tools.showToast('登录成功');
					uni.setStorageSync('token', res.data.token);
					uni.removeStorageSync('lastOpenTime');
					setTimeout(() => {
						goEasy()
					}, 1000);
					// 获取个人信息  存到本地
					getUserInfo().then(res => {
						uni.setStorageSync('userInfo', res.data)
						this.$store.state.userInfo = res.data
						// #ifdef APP-PLUS
						try {

							this.$store.dispatch('createZIM').then((zim_x) => {
								that.getZIM(res.data, that);
							});
						} catch (error) {
							console.log('22222222222222222', error);
						}
						// #endif
					})
					uni.switchTab({
						url: '/pages/index/index'
					});
				} else {
					that.codeColor = '#ff0000';
					setTimeout(function () {
						that.form.captcha = [];
						event.target.value = '';
						that.codeColor = '#313131';
					}, 1500);
					this.$tools.showToast(res.msg);
				}
			})
				.catch((err) => {
					this.$tools.showToast(err);
				});
		},
		anewSend() {
			var data = {
				mobile: this.form.mobile, //	是	string	手机号
				event: 'mobilelogin' //是	string	事件:register=注册,mobilelogin=验证码登录,changemobile=更改手机号,resetpwd=找回密码
			};

			smsSend(data).then((res) => {
				if (res.code == 1) {
					this.$tools.showToast('短信发送成功');
					clearInterval(this.clock);
					//重新开启倒计时
					this.countDown = 60;
					this.clock = setInterval((res) => {
						if (this.countDown > 0) {
							this.countDown--;
						} else {
							this.countDown = 60;
							clearInterval(this.clock);
						}
					}, 1000);
				} else {
					this.$tools.showToast(res.msg);
				}
			})
				.catch((err) => {
					console.log('错误回调', err);
				});
		},


		getZIM(userInfo, that) {
			// 注册监听“运行时错误信息”的回调
			that.$store.state.zim.on("error", function (zim, errorInfo) {
				console.log("error", errorInfo.code, errorInfo.message);
			});

			// 更新会话列表
			that.$store.state.zim.on("conversationChanged", function (zim, {
				infoList
			}) {
				that.$store.commit('updata_ConversationList', infoList)
			});

			// 注册监听“网络连接状态变更”的回调
			that.$store.state.zim.on(
				"connectionStateChanged",
				function (zim, {
					state,
					event,
					extendedData
				}) {
					// 当长时间网络连接断开导致SDK内部登出时，需要重新登录
					if (state == 0 && event == 3) {
						console.log("网络连接状态变更");
						that.$store.dispatch('ZIMLogin')
					}
				}
			);

			// 注册监听“令牌即将过期”的回调
			that.$store.state.zim.on("tokenWillExpire", function (zim, {
				second
			}) {
				// console.log("令牌即将过期", second);
				// 可以在这里调用 renewToken 接口来更新 token
				// 新 token 生成可以参考上文
				let token = uni.getStorageSync("zegoToken");
				uni.removeStorageSync("zegoToken");
				zim.renewToken(token)
					.then(function ({
						token
					}) {
						// 更新成功
						uni.setStorageSync("zegoToken", token);
						that.$store.dispatch('ZIMLogin')
					})
					.catch(function (err) {
						// 更新失败
						that.$store.dispatch('ZIMLogin')
					});
			});


			that.$store.dispatch('ZIMLogin')
		},


		//请求企服过滤数据数据
		filtration() {
			homeApi.filtration().then(res => {
				let arr = res.data;
				let userIds = arr.map(item => item.user_id);
				//存到vuex中
				this.$store.commit('set_filtration', userIds)
			})
		},
	},
	onLoad(options) {
		this.form.mobile = options.tel;
		this.form.app_version = uni.getStorageSync('version');
		this.clock = setInterval((res) => {
			if (this.countDown > 0) {
				this.countDown--;
			} else {
				this.countDown = 60;
				clearInterval(this.clock);
			}
		}, 1000);
		this.filtration()
	},
}